import { Toast } from 'antd-mobile';
import { reqAddList, reqShopDel, reqShopedit } from "../../http/api";
import { createSelector } from 'reselect'
// 1.初始值
const initState = {
    // 1.1初始化数据
    shopList: [],
    // 初始化全选状态---默认为false
    all: false,
}
let isRequest = true

// types
const types = {
    // 1.2 shopList的type
    SHOP_CHANGE_LIST: "SHOP_CHANGE_LIST",
    SHOP_ALL: "SHOP_ALL",
}
// reducer
export const shopReducer = (state = initState, action) => {
    // 1.3 
    switch (action.type) {
        case types.SHOP_CHANGE_LIST:
            return {
                ...state,
                shopList: action.shopList
            }
            break;
        case types.SHOP_ALL:
            return {
                ...state,
                all: action.all
            }
            break;
        default:
            return state
    }
}
// actions
export const actions = {
    // 1.4
    changeShopList: shopList => ({ type: types.SHOP_CHANGE_LIST, shopList }),
    changeAll: all => ({ type: types.SHOP_ALL, all }),
    // 1.6发列表请求
    reqShopListAction: () => {
        return (dispatch, getState) => {
            let { info: { uid } } = getState().login
            console.log(uid);
            let { shopList } = getState().shop
            // 发请求
            reqAddList({ uid: uid }).then(res => {
                if (res.data.code === 200) {
                    let arr = res.data.list[1].content ? res.data.list[1].content : []
                    // arr补checked---默认为false
                    // 由于每次修改后，请求列表将checked都变为false,但是我们希望checked是修改之前的状态。
                    arr.forEach(item => {
                        let obj = shopList.find((i) => i.id === item.id)
                        item.checked = obj ? obj.checked : false
                    })
                    // arr.forEach(item => item.checked = false)
                    dispatch(actions.changeShopList(arr))
                    // 下次发请求，打开开关
                    isRequest = true
                }
            })
        }
    },
    // 购物车点击其中某一个选中
    changeOne: (index) => {
        return (dispatch, getState) => {
            // 将列表取出来
            let { shopList } = getState().shop
            shopList[index].checked = !shopList[index].checked
            // 修改了shoplist
            dispatch(actions.changeShopList([...shopList]))
            // 修改了全选
            dispatch(actions.changeAll(shopList.every(item => item.checked)))
        }
    },
    // 修改全选
    allChecked: () => {
        return (dispatch, getState) => {
          
            // 将全选状态取出来
            let { all, shopList } = getState().shop
            if(shopList.length === 0){
                all = !all
            }
            dispatch(actions.changeShopList([...shopList]))
            all = !all;
            dispatch(actions.changeAll(all))
            // 修改shopList
            shopList.forEach(item => item.checked = all)
            dispatch(actions.changeShopList([...shopList]))
        }
    },
    // 点击了删除
    delAction: (params) => {
        return (dispatch, getState) => {
            //发请求
            reqShopDel(params).then(res => {
                if (res.data.code === 200) {
                    // 弹成功
                    Toast.success(res.data.msg)
                    dispatch(actions.reqShopListAction())
                }
            })
        }
    },
    //数量自增
    shopAdd: (params) => {
        return (dispatch, getState) => {
            // 发请求
            reqShopedit(params).then(res => {
                if (res.data.code === 200) {
                    // 刷新列表
                    dispatch(actions.reqShopListAction())
                }
            })
        }
    },
    // 数量自减
    shopSub: (id, num) => {
        return (dispatch, getState) => {
            if (num <= 1) {
                Toast.info("宝贝不能再减少了");
                return;
            }
            // 由于请求是异步的，所以num是可以自减少到0甚至-1，所以需要使用 **节流** 解决。首先先定义一个变量isRequest ,默认是true 
            // 判断开关是否开着--开着就发请求
            if (isRequest) {
                //    开关关掉
                isRequest = false
                // 发请求
                reqShopedit({ id: id, type: 1 }).then(res => {

                    if (res.data.code === 200) {
                        // 刷新列表
                        dispatch(actions.reqShopListAction())
                    }
                })
            }

        }
    }

}


// 导出数据
// 1.5
export const getShopList = state => state.shop.shopList
// 导出全选
export const getAll = state => state.shop.all
export const getAllPrice = createSelector([getShopList], (shopList) => {
    console.log("===========计算了计算了计算了,只需要计算一次，所以要用到getState==============");
    return shopList.reduce((val, item) => {
        return item.checked ? val += item.price * item.num : val
    }, 0)
})